<template>
  <div class="container">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">一级页面</el-breadcrumb-item>
      <el-breadcrumb-item>二级页面</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="main-content">
      <template #header>
        <div class="card-header">
          <h2>上架产品</h2>
        </div>
      </template>

      <el-alert
          title="请选择您要上架的新品"
          type="info"
          :closable="false"
          class="mb-20"
      />

      <el-row :gutter="20">
        <el-col
            v-for="product in currentPageProducts"
            :key="product.id"
            :xs="24"
            :sm="12"
            :md="12"
            :lg="12"
            class="mb-20"
        >
          <el-card
              :body-style="{ padding: '0px' }"
              :class="{ 'selected-card': selectedProducts.has(product.id) }"
              @click="toggleProduct(product)"
          >
            <div class="product-item">
              <el-checkbox
                  v-model="selectedProducts"
                  :label="product.id"
                  @click.stop
              />
              <el-image
                  :src="product.image"
                  :alt="product.name"
                  fit="cover"
              >
                <template #error>
                  <div class="image-placeholder">
                    <el-icon><Picture /></el-icon>
                  </div>
                </template>
              </el-image>
              <div class="product-info">
                <h3>{{ product.name }}</h3>
                <p>{{ product.description }}</p>
                <p class="location">{{ product.location }}</p>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <div class="pagination-container">
        <el-pagination
            v-model:current-page="currentPage"
            :page-size="itemsPerPage"
            :total="totalItems"
            layout="prev, pager, next"
            @current-change="handlePageChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed } from 'vue'
import { Picture } from '@element-plus/icons-vue'
import type { Product } from '@/types'

// 商品数据
const products = ref<Product[]>([
  {
    id: 1,
    name: 'New York',
    image: '/images/ny-food-1.jpg',
    description: 'New York City, the most populous city in the United States',
    location: 'located in the state of New York.'
  },
  {
    id: 2,
    name: 'New York',
    image: '/images/ny-food-2.jpg',
    description: 'New York City, the most populous city in the United States',
    location: 'located in the state of New York.'
  },
  {
    id: 3,
    name: 'Chicago',
    image: '/images/chicago-food-1.jpg',
    description: 'Chicago\'s streets were laid out in a street grid that grew from the city\'s original townsite plat.',
    location: 'Illinois'
  },
  {
    id: 4,
    name: 'Chicago',
    image: '/images/chicago-food-2.jpg',
    description: 'Chicago\'s streets were laid out in a street grid that grew from the city\'s original townsite plat.',
    location: 'Illinois'
  }
])

// 分页相关
const itemsPerPage = 4
const currentPage = ref(1)
const selectedProducts = ref<Set<number>>(new Set())

// 计算总数量
const totalItems = computed(() => products.value.length)

// 当前页的商品
const currentPageProducts = computed(() => {
  const start = (currentPage.value - 1) * itemsPerPage
  const end = start + itemsPerPage
  return products.value.slice(start, end)
})

// 切换页面
const handlePageChange = (page: number) => {
  currentPage.value = page
}

// 选择/取消选择商品
const toggleProduct = (product: Product) => {
  if (selectedProducts.value.has(product.id)) {
    selectedProducts.value.delete(product.id)
  } else {
    selectedProducts.value.add(product.id)
  }
}
</script>

<style scoped>
.container {
  max-width: 1200px;
  margin: 20px auto;
  padding: 0 20px;
}

.main-content {
  margin-top: 20px;
}

.mb-20 {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h2 {
  margin: 0;
  font-size: 18px;
}

.product-item {
  display: flex;
  padding: 15px;
  gap: 15px;
  align-items: flex-start;
}

.product-item .el-checkbox {
  margin-top: 8px;
}

.product-item .el-image {
  width: 120px;
  height: 120px;
  flex-shrink: 0;
}

.image-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7fa;
}

.product-info {
  flex-grow: 1;
}

.product-info h3 {
  margin: 0 0 10px;
  font-size: 16px;
}

.product-info p {
  margin: 0;
  color: #666;
  font-size: 14px;
}

.location {
  margin-top: 8px;
  font-size: 12px !important;
  color: #999 !important;
}

.selected-card {
  border-color: var(--el-color-primary);
  background-color: var(--el-color-primary-light-9);
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

:deep(.el-card__body) {
  padding: 0;
}
</style>
